#include "RecordTimeDormDialog.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QFile>
#include <QTextStream>

RecordTimeDormDialog::RecordTimeDormDialog(QWidget* parent)
    : QDialog(parent)
{
    setWindowTitle(tr("Record Time and Dorm Info"));
    resize(400, 300);

    QVBoxLayout* layout = new QVBoxLayout(this);

    semesterEdit = new QLineEdit(this);
    dormitoryCountEdit = new QLineEdit(this);
    checkinPeriodEdit = new QLineEdit(this);
    dormitoryNumberEdit = new QLineEdit(this);
    dormitoryIdEdit = new QLineEdit(this);
    bedIdEdit = new QLineEdit(this);
    bedCountEdit = new QLineEdit(this);
    remainingBedCountEdit = new QLineEdit(this);

    layout->addWidget(new QLabel(tr("Current Semester:"), this));
    layout->addWidget(semesterEdit);
    layout->addWidget(new QLabel(tr("Number of Dormitory Buildings:"), this));
    layout->addWidget(dormitoryCountEdit);
    layout->addWidget(new QLabel(tr("Available Check-in Period:"), this));
    layout->addWidget(checkinPeriodEdit);
    layout->addWidget(new QLabel(tr("Number of Dormitories:"), this));
    layout->addWidget(dormitoryNumberEdit);
    layout->addWidget(new QLabel(tr("Dormitory Number:"), this));
    layout->addWidget(dormitoryIdEdit);
    layout->addWidget(new QLabel(tr("Bed Number:"), this));
    layout->addWidget(bedIdEdit);
    layout->addWidget(new QLabel(tr("Number of Beds:"), this));
    layout->addWidget(bedCountEdit);
    layout->addWidget(new QLabel(tr("Remaining Number of Beds:"), this));
    layout->addWidget(remainingBedCountEdit);

    saveButton = new QPushButton(tr("Save"), this);
    cancelButton = new QPushButton(tr("Cancel"), this);
    QHBoxLayout* buttonLayout = new QHBoxLayout();
    buttonLayout->addWidget(saveButton);
    buttonLayout->addWidget(cancelButton);

    layout->addLayout(buttonLayout);

    connect(saveButton, &QPushButton::clicked, this, &RecordTimeDormDialog::save_data);
    connect(cancelButton, &QPushButton::clicked, this, &RecordTimeDormDialog::reject);
}

void RecordTimeDormDialog::save_data()
{
    QString data = QString("Current Semester: %1\n"
        "Number of Dormitory Buildings: %2\n"
        "Available Check-in Period: %3\n"
        "Number of Dormitories: %4\n"
        "Dormitory Number: %5\n"
        "Bed Number: %6\n"
        "Number of Beds: %7\n"
        "Remaining Number of Beds: %8\n")
        .arg(semesterEdit->text())
        .arg(dormitoryCountEdit->text())
        .arg(checkinPeriodEdit->text())
        .arg(dormitoryNumberEdit->text())
        .arg(dormitoryIdEdit->text())
        .arg(bedIdEdit->text())
        .arg(bedCountEdit->text())
        .arg(remainingBedCountEdit->text());

    QFile file("main_message.txt");
    if (file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        QTextStream out(&file);
        out << data;
        file.close();
    }

    accept();
}